package com.zxc.oauth.config

import com.zxc.oauth.model.CurrentAdmin
import com.zxc.oauth.properties.OauthAdminProperties
import com.zxc.oauth.service.TokenService
import com.zxc.oauth.service.impl.AdminTokenService
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty
import org.springframework.boot.context.properties.EnableConfigurationProperties
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration

@Configuration
@EnableConfigurationProperties(value = [OauthAdminProperties::class])
@ConditionalOnClass(OauthAdminConfig::class)
@ConditionalOnProperty(
    prefix = "oauth.admin",
    value = ["enabled"],
    matchIfMissing = true
)
class OauthAdminConfig {

    @Autowired
    private lateinit var oauthAdminProperties: OauthAdminProperties

    @Bean
    fun adminTokenService(): TokenService<CurrentAdmin>?{
        if(oauthAdminProperties.accessTokenSecret == null
            || oauthAdminProperties.refreshTokenSecret == null
            || oauthAdminProperties.accessTokenExpiresIn == null
            || oauthAdminProperties.refreshTokenExpiresIn == null){
            return null
        }
        return AdminTokenService(oauthAdminProperties)
    }
}